package net.huashitong.supervision.appController;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.databean.SysUserDataBean;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import com.sirdc.modules.utils.date.DateUtils;
import net.huashitong.supervision.task.entity.ReadDocEntity;
import net.huashitong.supervision.task.entity.SpecialTask;
import net.huashitong.supervision.task.entity.TaskMain;
import net.huashitong.supervision.task.entity.TaskUnit;
import net.huashitong.supervision.task.filter.SpecialTaskFilter;
import net.huashitong.supervision.task.filter.TaskMainFilter;
import net.huashitong.supervision.task.filter.TaskUnitFilter;
import net.huashitong.supervision.task.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/12/26
 * Time: 16:23
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Controller
@RequestMapping(value = "/app/leader")
public class AppLeaderController extends JsonBaseController {
    @Autowired
    private ReadDocService readDocService;
    @Autowired
    private CopeToService copeToService;
    @Autowired
    private TaskMainService taskMainService;
    @Autowired
    private TaskUnitService taskUnitService;
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private TaskCategoryService taskCategoryService;
    @Autowired
    private SpecialTaskService specialTaskService;

    @Override
    protected String getView(String s) {
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/readDocList",method = RequestMethod.POST)
    public Message readDocList(@RequestBody ReadDocEntity entity) {
        List<ReadDocEntity> list = readDocService.getByLeaderId(entity.getLeaderId());
        if (list.size() > 0) {
            return coverMessage("200", "成功", list);
        } else {
            return coverMessage("500", "无数据");
        }
    }

    /**
     * 修改呈阅件已读状态
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateRDStatus",method = RequestMethod.POST)
    public Message updateRDStatus(@RequestBody ReadDocEntity entity) {
        try {
            String sysId = entity.getSysId();
            ReadDocEntity readDocEntity = readDocService.getById(sysId);
            //已读
            readDocEntity.setStatus("1");
            readDocService.update(readDocEntity);
            return coverMessage("200", "200");
        } catch (Exception e) {
            e.printStackTrace();
            return coverMessage("500", "500");
        }
    }

    /**
     * 领导批示评价呈阅件;
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateReadDoc",method = RequestMethod.POST)
    public Message updateReadDoc(@RequestBody ReadDocEntity entity) {
        try {
            ReadDocEntity byId = readDocService.getById(entity.getSysId());
            byId.setIndicate(entity.getIndicate());
            byId.setIndicateTime(DateUtils.getDate());
            readDocService.update(byId);
            return coverMessage("200", "成功");
        } catch (Exception e) {
            e.printStackTrace();
            return coverMessage("500", "失败");
        }
    }

    /**
     * 抄送给领导任务列表
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/copeToList", method = RequestMethod.POST)
    public Message copeToList(@RequestBody TaskMainFilter filter) {
        try {
            String leaderId = filter.getCopeTo();//抄送领导ID
            List<TaskMain> taskMainList = copeToService.queryAllDone(filter);
            return coverMessage("200", "抄送任务列表", taskMainList);
        } catch (Exception e) {
            e.printStackTrace();
            return coverMessage("500", "error");
        }
    }

    /**
     * 抄送二级列表
     *
     * @param filter userId 当前用户ID     taskMainId  主任务ID
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/copeUnit", method = RequestMethod.POST)
    public Message copeUnit(@RequestBody TaskUnitFilter filter) {
        List<TaskUnit> copeUnit = taskUnitService.updateCopeTaskUnit(filter);
        return coverMessage("200", "抄送二级列表", copeUnit);
    }

    /**
     * 当前领导创建的所有任务
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/myTask",method = RequestMethod.POST)
    public Message myTask(@RequestBody TaskMainFilter filter) {
        try {
            String createUser = filter.getCreateUser();//当前用户ID.创建该任务的用户ID
            List<TaskMain> taskMainList = taskMainService.queryFilter(filter);
            return coverMessage("200", "我的任务列表", taskMainList);
        } catch (Exception e) {
            e.printStackTrace();
            return coverMessage("500", "error");
        }
    }

    /**
     * 县处级领导得分排名
     *
     * @param dataBean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/leaderScore", method = RequestMethod.POST)
    public Message leaderScore(@RequestBody SysUserDataBean dataBean) {
        try {
            SysUserFilter sysUserFilter = new SysUserFilter();
            sysUserFilter.setRoleId("01");
            sysUserFilter.setDeptId(dataBean.getDeptId());
            if("1".equals(dataBean.getDeptId())){
                //县委
                sysUserFilter.setDeptId("20190720100000000085");
            }else if("2".equals(dataBean.getDeptId())){
                //县委
                sysUserFilter.setDeptId("20190720100000000086");
            }else if("3".equals(dataBean.getDeptId())){
                //县政府
                sysUserFilter.setDeptId("20190909100000000103");
            }else if("4".equals(dataBean.getDeptId())){
                //县政协
                sysUserFilter.setDeptId("20190720100000000087");
            }
            SpecialTaskFilter specialTaskFilter = new SpecialTaskFilter();

            //县处级领导列表
            List<SysLogin> sysLogins = sysLoginService.queryUserLoginByRoleDept(sysUserFilter);
            List<Map<String,Object>> resultList= new ArrayList<>();
            for (SysLogin sysLogin : sysLogins) {
                List<TaskMain> zhongdians=new ArrayList<>();
                String sysId = sysLogin.getSysId();
                TaskMainFilter taskMainFilter = new TaskMainFilter();
                taskMainFilter.setCopeTo(sysId);
                List<TaskMain> taskMainList = copeToService.queryAllDone(taskMainFilter);
                int zhongdian= 0;
                int pishijian= 0;
                int gzpishijian=0;
                for (TaskMain taskMain : taskMainList) {
                    //重点
                    if ("20190919100000000001".equals(taskMain.getCategoryId())){
                        /*
                         * 修改后的得分计算规则
                         * */
                        int scoreNum=0;//平均分,当前主任务得分
                        int scores=0;//每轮任务相加总分
                        specialTaskFilter.setTaskMainId(taskMain.getSysId());
                        // 当前任务所有的特殊任务的集合
                        List<SpecialTask> specialTaskList = specialTaskService.queryFilter(specialTaskFilter);
                        for (SpecialTask specialTask : specialTaskList) {//每轮单位任务的得分
                            String score = specialTask.getScore();
                            if (StringUtils.isNotBlank(score)) {
                                int s = Integer.parseInt(score);
                                //当前任务总得分
                                scores+=s;
                            }
                        }
                        //当前主任务得分
                        if (scores>0){
                            //有分数,取平均分
                            scoreNum = scores / specialTaskList.size();
                        }else{
                            //没有.总分为0
                        }
                        //当前重点任务得分为scoreNum;
                        zhongdian+=scoreNum;
                    }else if ("20190807100000000005".equals(taskMain.getCategoryId())&&"1".equals(taskMain.getLeaderFollow())){
                        //已关注批示件
                        gzpishijian++;
                    }
                }
                String type = taskCategoryService.getById("20190919100000000001").getType();
                /*
                * 修改后的得分计算规则
                * */

              /*  zhongdians.forEach(task->{
                    int scoreNum=0;//平均分,当前主任务得分
                    int scores=0;//每轮任务相加总分
                    specialTaskFilter.setTaskMainId(task.getSysId());
                    // 当前任务所有的特殊任务的集合
                    List<SpecialTask> specialTaskList = specialTaskService.queryFilter(specialTaskFilter);
                    for (SpecialTask specialTask : specialTaskList) {//每轮单位任务的得分
                        String score = specialTask.getScore();
                        if (StringUtils.isNotBlank(score)) {
                            int s = Integer.parseInt(score);
                            //当前任务总得分
                            scores+=s;
                        }
                    }
                    //当前主任务得分
                    if (scores>0){
                        //有分数,取平均分
                         scoreNum = scores / specialTaskList.size();
                    }else{
                        //没有.总分为0
                    }
                    //当前重点任务得分为scoreNum;
                });*/
                // 重点得分加批示件得分
                String scoresNum=zhongdian+gzpishijian+"";

                Map<String,Object> map= new HashMap<>();
                map.put("name", sysLogin.getUsername());
                map.put("login", sysLogin);
                map.put("score",scoresNum);
                resultList.add(map);
            }
            return coverMessage("200","领导得分",resultList);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return coverMessage("500","数据异常");
        }
    }

    /**
     * 领导得分详情
     * @param dataBean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryScoreDetail",method = RequestMethod.POST)
    public Message queryScoreDetail(@RequestBody SysUserDataBean dataBean){
        try {
            SysLogin sysLogin = sysLoginService.getById(dataBean.getUserId());
            List<TaskMain> zhongdians=new ArrayList<>();
            String sysId = sysLogin.getSysId();
            TaskMainFilter taskMainFilter = new TaskMainFilter();
            taskMainFilter.setCopeTo(sysId);
            List<TaskMain> taskMainList = copeToService.queryAllDone(taskMainFilter);
            int zhongdian= 0;
            int pishijian= 0;
            int gzpishijian=0;
            SpecialTaskFilter specialTaskFilter = new SpecialTaskFilter();
            for (TaskMain taskMain : taskMainList) {
                //重点
                if ("20190919100000000001".equals(taskMain.getCategoryId())){
                 /*   zhongdian++;
                    zhongdians.add(taskMain);*/
                    /*
                     * 修改后的得分计算规则
                     * */
                    int scoreNum=0;//平均分,当前主任务得分
                    int scores=0;//每轮任务相加总分
                    specialTaskFilter.setTaskMainId(taskMain.getSysId());
                    // 当前任务所有的特殊任务的集合
                    List<SpecialTask> specialTaskList = specialTaskService.queryFilter(specialTaskFilter);
                    for (SpecialTask specialTask : specialTaskList) {//每轮单位任务的得分
                        String score = specialTask.getScore();
                        if (StringUtils.isNotBlank(score)) {
                            int s = Integer.parseInt(score);
                            //当前任务总得分
                            scores+=s;
                        }
                    }
                    //当前主任务得分
                    if (scores>0){
                        //有分数,取平均分
                        scoreNum = scores / specialTaskList.size();
                    }else{
                        //没有.总分为0
                    }
                    //当前重点任务得分为scoreNum;
                    zhongdian+=scoreNum;
                }/*else if ("20190807100000000005".equals(taskMain.getCategoryId())){
                    pishijian++;
                    zhongdians.add(taskMain);
                }*/else if ("20190807100000000005".equals(taskMain.getCategoryId())&&"1".equals(taskMain.getLeaderFollow())){
                    //已关注批示件
                    gzpishijian++;
                }
            }
            String type = taskCategoryService.getById("20190919100000000001").getType();
            String zdScore=zhongdian+"";
            String pishi=gzpishijian+"";
            Map<String,Object> map= new HashMap<>();
            map.put("name", sysLogin.getUsername());
            map.put("zdScore",zdScore);
            map.put("pishijian", pishi);
            return coverMessage("200","得分详情",map);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return coverMessage("500",e.getMessage());
        }
    }

    /**
     *  得分任务列表
     * @param dataBean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryTask",method = RequestMethod.POST)
    public Message queryTask(@RequestBody SysUserDataBean dataBean){
        try {
            String userId = dataBean.getUserId();
            SysLogin sysLogin = sysLoginService.getById(userId);
            TaskMainFilter taskMainFilter = new TaskMainFilter();
            taskMainFilter.setCopeTo(sysLogin.getSysId());
            List<TaskMain> taskMainList = copeToService.queryAllDone(taskMainFilter);
            int zhongdian= 0;
            int pishijian= 0;
            int gzpishijian=0;
            List<TaskMain> zhongdians=new ArrayList<>();
            List<TaskMain> pishijians=new ArrayList<>();
            for (TaskMain taskMain : taskMainList) {
                //重点
                if ("20190919100000000001".equals(taskMain.getCategoryId())){
                    taskMain.setCategoryId(taskCategoryService.getById(taskMain.getCategoryId()).getCategory());
                    zhongdian++;
                    zhongdians.add(taskMain);
                }/*else if ("20190807100000000005".equals(taskMain.getCategoryId())){
                    pishijian++;
                    zhongdians.add(taskMain);
                }*/else if ("20190807100000000005".equals(taskMain.getCategoryId())&&"1".equals(taskMain.getLeaderFollow())){
                    taskMain.setCategoryId(taskCategoryService.getById(taskMain.getCategoryId()).getCategory());
                    //已关注批示件
                    gzpishijian++;
                    pishijians.add( taskMain);
                }
            }
            Map <String,Object> resultMap=new HashMap<>();

            if ("1".equals(dataBean.getDemo())){
                resultMap.put("list", zhongdians);
            }else if ("2".equals(dataBean.getDemo())){
                resultMap.put("list", pishijians);
            }
            return coverMessage("200","任务列表",resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return coverMessage("500",e.getMessage());
        }
    }
    /**
     * 绩效调用领导得分详情
     * @param dataBean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryLeaderScoreDetail",method = RequestMethod.POST)
    public Message queryLeaderScoreDetail(@RequestBody SysUserDataBean dataBean){
        try {
            List<Map<String,Object>>resultList= new ArrayList<>();
            String mobile = dataBean.getMobile();
            List<SysUserDataBean> dataBeans = sysUserService.queryUserDateBeans(mobile);
            if (ObjectUtils.isNotBlank(dataBeans)){
                for (SysUserDataBean bean : dataBeans) {
                    SysLogin sysLogin = sysLoginService.getById(bean.getUserId());
                    List<TaskMain> zhongdians=new ArrayList<>();
                    String sysId = sysLogin.getSysId();
                    TaskMainFilter taskMainFilter = new TaskMainFilter();
                    taskMainFilter.setCopeTo(sysId);
                    List<TaskMain> taskMainList = copeToService.queryAllDone(taskMainFilter);
                    int zhongdian= 0;
                    int pishijian= 0;
                    int gzpishijian=0;
                    for (TaskMain taskMain : taskMainList) {
                        //重点
                        if ("20190919100000000001".equals(taskMain.getCategoryId())){
                            zhongdian++;
                            zhongdians.add(taskMain);
                        }else if ("20190807100000000005".equals(taskMain.getCategoryId())&&"1".equals(taskMain.getLeaderFollow())){
                            //已关注批示件
                            gzpishijian++;
                        }
                    }
                    String type = taskCategoryService.getById("20190919100000000001").getType();
                    String zdScore=zhongdians.size()*Integer.parseInt(type)+"";
                    String pishi=gzpishijian+"";
                    Map<String,Object> map= new HashMap<>();
                    map.put("name", sysLogin.getUsername());
                    map.put("leader", bean);
                    map.put("zdScore",zdScore);
                    map.put("pishijian", pishi);
                    resultList.add(map);
                }
                return coverMessage("200","得分详情",resultList);
            }else{
                return coverMessage("500","用户不存在");
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return coverMessage("500",e.getMessage());
        }
    }
}


